package com.tamako.maaremote.controller;


import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tamako.maaremote.model.Content;
import com.tamako.maaremote.model.Response;
import com.tamako.maaremote.model.StartTask;
import com.tamako.maaremote.model.TaskFrom;
import com.tamako.maaremote.model.po.Task;
import com.tamako.maaremote.model.po.User;
import com.tamako.maaremote.service.TaskService;
import com.tamako.maaremote.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Tamako
 * @data 2024/8/17 11:49
 */
@RestController
@RequestMapping("/v1")
@Slf4j
public class TaskController {

    @Resource
    private TaskService taskService;

    @Resource
    private UserService userService;

    @GetMapping("/myStart")
    public String myStart() {
        Task task = new Task();
        task.setId(UUID.randomUUID().toString());
        task.setType("LinkStart");
        task.setUserid("1253594122");
        taskService.save(task);
        return "task created，ID：" + task.getId();
    }


    @GetMapping("/start")
    public String start(@RequestBody StartTask startTask) {

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserid, startTask.getUserid());
        User user = userService.getOne(queryWrapper);
        if (user == null) {
            //新用户
            user = new User();
            user.setUserid(startTask.getUserid());
            user.setDevice(startTask.getDevice());
            userService.save(user);
        }
        if (user.getDevice().equals(startTask.getDevice())) {
            //设备一致
            Task task = new Task();
            task.setId(UUID.randomUUID().toString());
            task.setType("LinkStart");
            task.setUserid(user.getUserid());
            taskService.save(task);
            return "task created，ID：" + task.getId();
        } else {
            return "This user is bound to a device";
        }

    }

    @PostMapping("/getTask")
    public Response getTask(@RequestBody Content content) {
        //判断当前用户的设备号是否正确
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserid, content.getUser());
        User user = userService.getOne(queryWrapper);
        if (user == null) {
            //用户不存在
            return null;
        }
        if (!user.getDevice().equals(content.getDevice())) {
            //设备不一致
            return null;
        }
        //查询当前用户的任务
        LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Task::getStatus, 1);
        wrapper.eq(Task::getUserid, content.getUser());
        List<Task> tasks = taskService.list(wrapper);
        if (!tasks.isEmpty()) {
            List<TaskFrom> taskFroms = new ArrayList<>();
            List<Task> taskList = new ArrayList<>();
            for (Task task : tasks) {
                TaskFrom taskFrom = new TaskFrom();
                taskFrom.setId(task.getId());
                taskFrom.setType(task.getType());
                taskFroms.add(taskFrom);
                task.setStatus(2);
                taskList.add(task);
            }
            Response response = new Response();
            response.setTasks(taskFroms);
            log.info("用户：{}，开始执行任务", content.getUser());
            taskService.updateBatchById(taskList);
            return response;
        }
        return null;
    }

    @PostMapping("/reportStatus")
    public String reportStatus(@RequestBody Content content) {
        log.info("用户：{}，任务ID：{}，状态：{}，内容：{}", content.getUser(), content.getTask(), content.getStatus(), content.getPayload());
        //更新任务状态
        Task task = taskService.getById(content.getTask());
        if (task == null) {
            //任务不存在
            return "任务不存在";
        }
        if (!task.getUserid().equals(content.getUser())) {
            //用户不一致
            return "用户不一致";
        }
        task.setStatus(3);
        task.setPayload(content.getPayload());
        taskService.updateById(task);
        return "200OK";
    }
}
